<?php

namespace app\api\controller;

use app\common\controller\UnLogin;
use app\common\service\UpService;


class Upload extends UnLogin {

	// public function upload_img() {
	// 	$image = $_FILES["file"];
	// 	if(!$image){
	// 		$this->retData(1,'没有上传图片');
	// 	}
	// 	$res   = UpService::Upp($image["tmp_name"]);
	// 	$this->retData(0,'success',$res);
	// }

	// public function upload_img2() {
	// 	$image = $_FILES["file"];
	// 	if(!$image){
	// 		$this->retData(1,'没有上传图片');
	// 	}
	// 	$res   = UpService::Upp($image["tmp_name"]);
 //         $this->retData(0,'success',['src'=>$res]);
	// }

	// public function cj(){
	// 	UpService::cj();
	// }

	public function upload_img(){
		$type       = request()->param('type')?:1;
		$image      = request()->file('"file"');
		$image2      = $_FILES["file"];
		switch ($type) {
			case 1:
				$path = '/upload/';
				break;
			
			default:
				# code...
				break;
		}
		$validate = ['size'=>20*1024*1024,'ext'=>'jpg,png,gif,jpeg,bmp,png'];
		$res = $image->validate($validate)->move('../public'.$path);
		if ($res) {
			// var_dump($res);die();
            $name = $res->getSaveName();
			// $name = $res->filename();
			$save_name = '../public'.$path.$name;
            $name = str_replace('\\', '/', $name);
            //图片压缩
            $compress = new \Picture\Compress($save_name,0.5);
            $img_name = explode('.', $name);
            $new_name = $img_name[0].'2.'.$img_name[1];
            $compress->compressImg('../public'.$path.$new_name);
            $name = C('system_url').$path.$new_name;
            $this->retData(0,'success',$name);
        } else {
            $this->retData(1,'图片上传失败');
        }
	}

	public function upload_img2(){
		$type       = request()->param('type')?:1;
		$image      = request()->file('"file"');
		// $image      = $_FILES["file"];
		switch ($type) {
			case 1:
				$path = '/upload/';
				break;
			
			default:
				# code...
				break;
		}
		$validate = ['size'=>20*1024*1024,'ext'=>'jpg,png,gif,jpeg,bmp,png'];
		$res = $image->validate($validate)->move('../public'.$path);
		if ($res) {
			// var_dump($res);die();
            $name = $res->getSaveName();
			// $name = $res->filename();
			$save_name = '../public'.$path.$name;
            $name = str_replace('\\', '/', $name);
            //图片压缩
            $compress = new \Picture\Compress($save_name,0.5);
            $img_name = explode('.', $name);
            $new_name = $img_name[0].'2.'.$img_name[1];
            $compress->compressImg('../public'.$path.$new_name);
            $name = C('system_url').$path.$new_name;
            $this->retData(0,'success',['src'=>$name]);
        } else {
            $this->retData(1,'图片上传失败');
        }
	}
}